package com.layblog.templates;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.layblog.common.template.DirectiveHandler;
import com.layblog.common.template.TemplateDirective;
import com.layblog.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 文章列表模板
 *
 * @author: jacklin
 * @since 2022/8/12 0:42
 **/
@Component
public class PostsTemplate extends TemplateDirective {

    @Autowired
    PostService postService;

    @Override
    public String getName() {
        return "posts";
    }

    @Override
    public void execute(DirectiveHandler handler) throws Exception {
        Long categoryId = handler.getLong("categoryId");
        Integer pn = handler.getInteger("pn", 1);
        Integer size = handler.getInteger("size", 5);
        Integer level = handler.getInteger("level");

        Page page = new Page(pn, size);
        IPage results = postService.paging(page, null, categoryId, level, null, "created");

        handler.put(RESULTS, results).render();
    }
}
